package co.yixiang.controller.manager.monitoring;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.monitoring.entity.domain.YxMonitoringCategory;
import co.yixiang.modules.monitoring.entity.dto.YxMonitoringCategoryDto;
import co.yixiang.modules.monitoring.entity.query.YxMonitoringCategoryQueryCriteria;
import co.yixiang.modules.monitoring.service.YxMonitoringCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "监控:分类管理")
@RestController
@RequestMapping("api/monitoringCategory")
public class MonitoringCategoryController {

    @Autowired
    private IGenerator generator;
    @Autowired
    private YxMonitoringCategoryService categoryService;



    @ApiOperation("获取文章分类")
    @GetMapping(value = "/build")
    @PreAuthorize("hasAnyRole('admin','common','MONITORINGCATEGORY_ALL','MONITORINGCATEGORY_SELECT')")
    public ResponseEntity<Object> buildYxMonitoringCategorys() {
        final List<YxMonitoringCategoryDto> convert = generator.convert(categoryService.queryAll(null), YxMonitoringCategoryDto.class);
        // final Object content = categoryService.buildTree(convert).get("content");
        //return new ResponseEntity<>(categoryService.buildYxArticleCategorys(YxMonitoringCategoryDtos), HttpStatus.OK);
        return null;
    }

    @ApiOperation("返回全部的菜单")
    @GetMapping(value = "/tree")
    @PreAuthorize("hasAnyRole('admin','common','MONITORINGCATEGORY_ALL','MONITORINGCATEGORY_SELECT')")
    public ResponseEntity<Object> getYxMonitoringCategoryTree() {
        return new ResponseEntity<>(categoryService.getYxMonitoringCategoryTree(categoryService.findByPid(0)), HttpStatus.OK);
    }

    @ApiOperation(value = "查询分类")
    @GetMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORINGCATEGORY_ALL','MONITORINGCATEGORY_SELECT')")
    public ResponseEntity getYxMonitoringCategoryPage(YxMonitoringCategoryQueryCriteria criteria) {
        final List<YxMonitoringCategoryDto> convert = generator.convert(categoryService.queryAll(criteria), YxMonitoringCategoryDto.class);
        return new ResponseEntity<>(categoryService.buildTree(convert), HttpStatus.OK);
    }
    @ApiOperation(value = "新增")
    @PostMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORINGCATEGORY_ALL','MONITORINGCATEGORY_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YxMonitoringCategory resources) {
        return new ResponseEntity(categoryService.save(resources), HttpStatus.CREATED);
    }
    @ApiOperation(value = "修改")
    @PutMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORINGCATEGORY_ALL','MONITORINGCATEGORY_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YxMonitoringCategory resources) {
        categoryService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    @ApiOperation(value = "删除")
    @DeleteMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORINGCATEGORY_ALL','MONITORINGCATEGORY_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        categoryService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
